package com.boss.common;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 文件操作工具类，提供了读取和写入文件的方法
 * @author 郑炆杰
 */

public class FileUtils {
    private FileUtils() {
        throw new AssertionError("No FileUtils instances for you!");
    }

    /**
     * 读取指定路径的文件内容为字符串。
     *
     * @param filePath 文件路径
     * @return 文件内容字符串
     * @throws IOException 如果读取文件过程中发生 IO 错误
     */
    public static String readFile(String filePath) throws IOException {
        StringBuilder fileContent;
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            fileContent = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                fileContent.append(line).append("\n");
            }
        }
        return fileContent.toString();
    }

    public static void writeFile(String filePath, String content) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        }
    }
}
